package mp

import (
	"duoke/model"
	"duoke/service"
	"duoke/service/common"
	"duoke/utils/gconv"
	"duoke/utils/response"
	"duoke/utils/valid"
	"fmt"
	"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"io"
	"net/http"
)

var Qrcode = new(qrcodeCtl)

type qrcodeCtl struct{}

func (ctl *qrcodeCtl) Create(c *gin.Context) {
	atype := c.DefaultQuery("type", "")
	id := c.DefaultQuery("id", "")
	gid := c.DefaultQuery("gid", "")
	autoColor := gconv.Int(c.DefaultQuery("auto_color", "0"))
	isHyaline := gconv.Int(c.DefaultQuery("is_hyaline", "1"))
	fid := c.DefaultQuery("fid", "")
	r := gconv.Int(c.DefaultQuery("r", "0"))
	g := gconv.Int(c.DefaultQuery("g", "0"))
	b := gconv.Int(c.DefaultQuery("b", "0"))
	goodsId := c.DefaultQuery("goodsId", "")
	groupId := c.DefaultQuery("groupId", "")
	fromId := c.DefaultQuery("fromId", "")
	orderId := c.DefaultQuery("orderId", "")
	var path string
	var scene string
	c.Set("account", service.AccountStruct{Id: 1})
	if atype != "share" && atype != "hexiao" && atype != "share_coupon" && atype != "hx_coupon" && atype != "give_coupon" && atype != "merchant_money_receive" && atype != "user_money_receive" && atype != "score_receive" {
		c.String(http.StatusOK, "type错误")
		return
	}
	if atype == "share" {
		if goodsId == "" {
			c.String(http.StatusOK, "goodsId错误")
			return
		}
		path = "pages/pintuan/goods/index"
		scene = goodsId + "_" + groupId + "_" + fromId
		fmt.Println(path, scene)
	}
	if atype == "hexiao" {
		if goodsId == "" {
			c.String(http.StatusOK, "goodsId错误")
			return
		}
		if orderId == "" {
			c.String(http.StatusOK, "orderId错误")
			return
		}
		path = "pages/pintuan/hexiao/index"
		scene = goodsId + "_" + orderId
	}
	if atype == "share_coupon" {
		if gid == "" {
			c.String(http.StatusOK, "gid错误")
			return
		}
		path = "pages/coupon/goods/index"
		scene = gid + "_" + fid
		fmt.Println(path, scene)
	}
	if atype == "give_coupon" {
		if id == "" {
			c.String(http.StatusOK, "id错误")
			return
		}
		path = "pages/coupon/give/index"
		scene = id
		fmt.Println(path, scene)
	}
	if atype == "hx_coupon" {
		if id == "" {
			c.String(http.StatusOK, "ID缺失")
			return
		}
		path = "pages/coupon/hexiao/index"
		scene = id
	}
	if atype == "score_receive" {
		if id == "" {
			c.String(http.StatusOK, "ID缺失")
			return
		}
		path = "pages/account/score/index"
		scene = id
	}
	if atype == "user_money_receive" {
		if id == "" {
			c.String(http.StatusOK, "ID缺失")
			return
		}
		path = "pages/account/user_money/index"
		scene = id
	}
	if atype == "merchant_money_receive" {
		if id == "" {
			c.String(http.StatusOK, "ID缺失")
			return
		}
		path = "pages/account/money/index"
		scene = id
	}
	miniApp, err := service.Miniprogram.App(c)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	unlimited, err := miniApp.WXACode.GetUnlimited(c, scene, path, false, "release", 430, autoColor == 1, &power.HashMap{"r": r, "g": g, "b": b}, isHyaline == 1)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	_, _ = io.Copy(c.Writer, unlimited.Body)
}
func (ctl *qrcodeCtl) GenerateCode(c *gin.Context) {
	var req model.GenerateCode
	if err := c.ShouldBindJSON(&req); err != nil {
		response.Error(c, valid.GetError(err.(validator.ValidationErrors), req), nil)
		return
	}
	where := fmt.Sprintf("path='%s' and param='%s'", req.Path, req.Param)
	var v1 model.ReqMiniCode
	get, err := common.Tales.Info(where, "url").Table("mini_program_code").Get(&v1)
	if err != nil {
		response.Error(c, err.Error(), nil)
		return
	}
	var url string
	if !get {
		v2, err := service.SeparateAccounts.GenerateCodeV2(c, &req)
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		m := map[string]interface{}{
			"path":  req.Path,
			"url":   v2.Url,
			"param": req.Param,
		}
		err = common.Tales.Save(c, m, "mini_program_code")
		if err != nil {
			response.Error(c, err.Error(), nil)
			return
		}
		url = v2.Url
	} else {
		url = v1.Url
	}

	response.Success(c, "success", url)
}
